const merge = require("webpack-merge");
const webpack = require("webpack");
const path = require("path");
const fs = require('fs');

const dir = require("./dir");

const pathToName = require(path.join(dir.frameworkCommonDir, 'driver')).pathToName;
const resolve = require("./driver").resolve;
const webpackPromise = require("./driver").webpackPromise;

const common = require("./webpack.common.js");


// 构建前清理
const CleanWebpackPlugin = require("clean-webpack-plugin");

module.exports = async (source_path, app) => {
    let source_name = pathToName(source_path);
    let public_name = source_name + '/bin/';
    let entry = {};
    entry[source_name] = path.join(resolve(source_path), 'src');

    let dll_configs = [
        new CleanWebpackPlugin(),
        // 纯静态资源复制
        // new CopyWebpackPlugin(copy_configs)
    ];

    let mainfest_file = path.join(dir.binDir, app || '', source_name, 'dll/index.manifest.json');

    if (fs.existsSync(mainfest_file))
        dll_configs.push(new webpack.DllReferencePlugin({
            manifest: require(mainfest_file)
        }));

    let config = merge(common, {
        mode: "development",
        entry: entry,
        output: {
            publicPath: public_name,
            path: path.join(dir.binDir, app || '', source_name, 'bin'),
            filename: "index.js",
            chunkFilename: "[name].[contenthash].js",
            library: `${source_name}`,
            libraryTarget: "umd"
        },
        // devtool: "source-map",
        plugins: dll_configs,
    });

    await webpackPromise(config);


};
